package pri.zey.custom.tools.image;/**
 * Created by Administrator on 2016/3/16.
 */

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.List;
import java.util.Set;

import pri.zey.custom.tools.Tio;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public abstract class ALoaderImagerThread<T> {

    protected final String TAG = "TAGSS";
    protected Set<NewsThread> mTasks;//任务集合管理
    protected LruCache<String, Bitmap> mCache;//缓存对象
    protected ListView mListView;//因为需要寻找出对相应的imageview来显示图片，所以需要获取当前listview控件使用findViewWidthTag(Object object)获取对应Tag的控件
    protected List<T> mData;//因为要按照滑动区域来显示图片内容,需要获取所有图片地址来按照指定范围显示图片

    //数据处理
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            String mURL = bundle.getString("url");
            Bitmap bitmap = (Bitmap) msg.obj;
            ImageView imageView = (ImageView) mListView.findViewWithTag(mURL);
            if(bitmap != null && imageView != null){
                imageView.setImageBitmap(bitmap);
            }
        }
    };

    protected ALoaderImagerThread(ListView mListView, List<T> mData){
        this.mData = mData;
        this.mListView = mListView;
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        int CacheSize = maxMemory / 4;
        mCache = new LruCache<String, Bitmap>(CacheSize){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getByteCount();
            }
        };
    }

    //把图片到缓存中
    protected void addBitmapToCache(String url, Bitmap bitmap){
        mCache.put(url, bitmap);
    }

    //从缓存中获取图片
    protected Bitmap getBitmapFromCache(String url){
        return mCache.get(url);
    }

    protected void setImageViewBitmap(String url){
        //判断图片是否有缓存，有的就直接获取缓存图片直接显示，没有就下载后加载
        Bitmap bitmap = getBitmapFromCache(url);
        if(bitmap == null){
            //如果没有缓存图片的话，执行下载和缓存图片
            NewsThread thread = new NewsThread(url);
            thread.start();
            mTasks.add(thread);
        }else{
            //如果有缓存图片的话，直接加载图片到视图中
            ImageView imageView = (ImageView) mListView.findViewWithTag(url);
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }

    //listview 不滑动时候取消其他task
    public void cancelAllTask(){
        if(mTasks != null){
            for (NewsThread task : mTasks){
                try {
                    task.clone();
                } catch (CloneNotSupportedException e) {
                    e.printStackTrace();
                }
            }
//            Log.i(TAG, "mTasks size : " + mTasks.size());
        }
    }

    //任务线程
    protected class NewsThread extends Thread implements Cloneable{
        private String mUrl;
        private boolean isRunstatu = true;

        NewsThread(String url){
            this.mUrl = url;
        }

        @Override
        public void run() {
            super.run();
            Bitmap bitmap = Tio.getBitmapFromURL(mUrl);
            if(bitmap != null){
                //获取图片成功，进行图片缓存
                addBitmapToCache(mUrl, bitmap);
            }
            //把bitmap发送到ui线程执行数据处理
            Message msg = Message.obtain();
            Bundle bundle = new Bundle();
            bundle.putString("url", mUrl);
            msg.setData(bundle);
            msg.obj = bitmap;
            mHandler.sendMessage(msg);
            //任务执行完毕，把任务集合中的当前执行完毕的任务remove
            mTasks.remove(this);
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
}
